package tech.xs.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.xs.auth.api.AuthPathPermissionApi;
import tech.xs.common.framework.domain.model.ApiResult;
import tech.xs.common.framework.service.CrudServiceImpl;
import tech.xs.common.framework.util.Assert;
import tech.xs.sys.dao.SysPermissionDao;
import tech.xs.sys.dao.SysRolePermissionDao;
import tech.xs.sys.domain.bo.permission.ImportExportPermissionBo;
import tech.xs.sys.domain.entity.SysPermission;
import tech.xs.sys.domain.entity.SysRolePermission;
import tech.xs.sys.service.SysPermissionService;
import tech.xs.sys.service.SysRolePermissionService;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysPermissionServiceImpl extends CrudServiceImpl<SysPermissionDao, SysPermission> implements SysPermissionService {

    @Resource
    private SysRolePermissionDao sysRolePermissionDao;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Resource
    private SysPermissionDao sysPermissionDao;
    @Resource
    private AuthPathPermissionApi authPathPermissionApi;


    @Override
    public boolean isUsed(String permissionCode) {
        if (sysRolePermissionDao.selectCount(Wrappers.<SysRolePermission>lambdaQuery().eq(SysRolePermission::getPermissionCode, permissionCode)) > 0) {
            return true;
        }
        ApiResult<Boolean> authPermissionResp = authPathPermissionApi.isUsedPermissionCode(permissionCode);
        return authPermissionResp.getData();
    }

    @Override
    public void deletePermissionByCode(String permissionCode) {
        Assert.isTrue(!isUsed(permissionCode), "权限已经被使用");
        sysPermissionDao.delete(Wrappers.<SysPermission>lambdaQuery().eq(SysPermission::getCode, permissionCode));
        sysRolePermissionDao.delete(Wrappers.<SysRolePermission>lambdaQuery().eq(SysRolePermission::getRoleCode, permissionCode));
        sysRolePermissionService.deleteEnableRolePermissionCache();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importPermission(List<ImportExportPermissionBo> importDatas) {
        for (ImportExportPermissionBo importData : importDatas) {
            boolean isExist = count(Wrappers.<SysPermission>lambdaQuery().eq(SysPermission::getCode, importData.getCode())) > 0;
            if (isExist) {
                update(Wrappers.<SysPermission>lambdaUpdate()
                        .set(SysPermission::getName, importData.getName())
                        .set(importData.getPermissionStatus() != null, SysPermission::getPermissionStatus, importData.getPermissionStatus())
                        .set(StrUtil.isNotBlank(importData.getRemark()), SysPermission::getRemark, importData.getRemark())
                        .set(StrUtil.isBlank(importData.getRemark()), SysPermission::getRemark, null)
                        .eq(SysPermission::getCode, importData.getCode())
                );
            } else {
                SysPermission saveData = new SysPermission();
                saveData.setCode(importData.getCode());
                saveData.setName(importData.getName());
                saveData.setPermissionStatus(importData.getPermissionStatus());
                saveData.setRemark(importData.getRemark());
                save(saveData);
            }
        }
        sysRolePermissionService.deleteEnableRolePermissionCache();
    }

}
